<?php
/**
 * Created by PhpStorm.
 * User: daimin
 * Date: 15/7/22
 * Time: 下午6:31
 */

namespace common\services\common;

use common\services\BaseService;
use common\repositories\common\MyscrmApplicationRepository;
use common\repositories\common\MyscrmOrganizationRepository;
use common\repositories\common\MyscrmPackageRepository;

/**
 * 应用授权套餐包接口类
 * Class MyscrmPackageService
 * @package common\services\common
 */
class MyscrmPackageService extends BaseService {

    private $_packageRepository = null;
    private $_organizationRepository = null;
    private $_myscrmApplicationRepository;

    public function __construct(MyscrmPackageRepository $packageRepository,
                                MyscrmOrganizationRepository $organizationRepository,
                                MyscrmApplicationRepository $myscrmApplicationRepository
    ){
        $this->_packageRepository = $packageRepository;
        $this->_organizationRepository = $organizationRepository;
        $this->_myscrmApplicationRepository = $myscrmApplicationRepository;
        parent::__construct();
    }

    /**
     * 返回所有package
     * @param string $sortField 排序字段
     * @param int $order 排序方向, 使用php常量
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getAllPackage($sortField='', $order=SORT_ASC){
        return $this->_packageRepository->getPackages($sortField, $order);
    }


    /**
     * 添加package
     * @param $pkgObj
     * @return bool
     * @throws \Exception
     */
    public function addPackage($pkgObj){
        return $this->_packageRepository->addPackage($pkgObj);
    }


    /**
     * 添加产品到产品包
     * @param $package_id
     * @param $application_id
     * @return bool
     * @throws \Exception
     */
    public function addApplicationToPackage($package_id, $application_id){
        // 检查package和application是否存在
        $pkg = $this->_packageRepository->getPackageById($package_id);
        if(empty($pkg)){
            throw new \Exception("不存在的package");
        }

        $app = $this->_myscrmApplicationRepository->getApplicationByApplicationId($application_id);
        if(empty($app)){
            throw new \Exception("不存在的application");
        }

        return $this->_packageRepository->addApplicationToPackage($package_id, $application_id);
    }

   /**
     * 删除掉package中无效的application
     */
    public function delNoUsePackageApplication(){

        $pkgs = $this->_packageRepository->getPackages();
        if(!empty($pkgs)){
            foreach($pkgs as $pkg){
                $paIds = $this->_packageRepository->getPackageApplicationIds($pkg['id']);
                foreach($paIds as $paId){
                    $app = $this->_myscrmApplicationRepository->getApplicationByApplicationId($paId['application_id']);
                    if(empty($app)){
                        // 如果存在就从package中删掉
                        $this->_packageRepository->delApplicationInPackage($pkg['id'], $paId['application_id']);
                    }
                }

            }
        }
        return true;
    }

    /**
     * 修改package的price
     * @param $package_id
     * @param $price
     * @param string $unit
     * @return bool
     * @throws \Exception
     */
    public function modifyPackagePrice($package_id, $price, $unit=''){

        // 查询package_id是否存在
        if(empty($package_id) || empty($price)){
            throw new \Exception('参数不能为空');
        }
        // price不能小于0
        if(!is_numeric($price)){
            throw new \Exception('price必须为数字');
        }

        if($price < 0){
            throw new \Exception('price不能小于0');
        }

        $pkg = $this->_packageRepository->getPackageById($package_id);
        if(empty($pkg)){
            throw new \Exception('不存在的package');
        }

        return $this->_packageRepository->upsertPackagePrice($package_id, $price, $unit);

    }


    /**
     * 得到package的价格
     * @param $package_id
     * @return null|static
     * @throws \Exception
     */
    public function getPackagePrice($package_id){
        if(empty($package_id)){
            throw new \Exception('参数不能为空');
        }

        $pkg = $this->_packageRepository->getPackageById($package_id);
        if(empty($pkg)){
            throw new \Exception('不存在的package');
        }

        return $this->_packageRepository->getPackagePrice($package_id);
    }

    /**
     * 得到package的价格根据包的unique_name
     * @param $packageName string 包的unique_name
     * @return null|static
     * @throws \Exception
     */
    public function getPackagePriceByName($packageName){
        if(empty($packageName)){
            throw new \Exception('参数不能为空');
        }

        $pkg = $this->_packageRepository->getPackageByUniqueName($packageName);
        if(empty($pkg)){
            throw new \Exception('不存在的package');
        }

        return $this->_packageRepository->getPackagePrice($pkg['id']);
    }

    /**
     * 得到package中application信息
     * @param $package_id
     * @return array
     * @throws \Exception
     */
    public function getApplicationInPackage($package_id){
        if(empty($package_id)){
            throw new \Exception('参数不能为空');
        }

        $pkg = $this->_packageRepository->getPackageById($package_id);
        if(empty($pkg)){
            throw new \Exception('不存在的package');
        }

        $apps = $this->_packageRepository->getPackageApplicationIds($package_id);
        $newApps = [];
        if(!empty($apps)){
            foreach ($apps as $app) {
                $appobj = $this->_myscrmApplicationRepository->getApplicationByApplicationId($app['application_id']) ;
                if(!empty($appobj)){
                    $newApps []= $appobj;
                }
           }

        }

        return $newApps;
    }


}